package org.budo.mail.util;

import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.budo.support.java.regex.pattern.factory.RegexPatternFactory;
import org.budo.support.lang.util.MapUtil;
import org.budo.support.lang.util.StringUtil;

/**
 * @author lmw
 */
public class MailAddressUtil {
    private static final String EMAIL_REQULAR = "^(([a-zA-Z0-9])+([\\_\\-\\.]){0,1})+([\\w\\-])+@{1}((([a-zA-Z0-9])+([\\-\\.]){0,1})*([a-zA-Z0-9])+)+[\\.]{1}[a-zA-Z]+$";

    private static final Pattern MAIL_ADDRESS_PATTERN = RegexPatternFactory.getInstance().getPattern(EMAIL_REQULAR, Pattern.CASE_INSENSITIVE);

    private static final Map<String, String> RECEIVE_HOSTS = MapUtil.newMap("@qq.com", "pop.qq.com", "@mail.com", "pop3.mail.com", "@infocare.cn", "mail.infocare.cn");

    private static final Map<String, String> SEND_HOSTS = MapUtil.newMap("@qq.com", "smtp.qq.com", "@mail.com", "smtp.mail.com", "@infocare.cn", "mail.infocare.cn", "@ewei.com", "mail.ewei.com");

    public static String guessReceiveHost(String address) {
        String suffix = getSuffix(address);
        String lowerCase = StringUtil.toLowerCase(suffix);
        return RECEIVE_HOSTS.get(lowerCase);
    }

    public static String getSuffix(String address) {
        if (StringUtil.isEmpty(address)) {
            return address;
        }
        int index = address.indexOf('@');
        if (index < 0) {
            throw new RuntimeException("can not get email suffix for address '" + address + "'");
        }
        return address.substring(index);
    }

    public static String getPrefix(String address) {
        int index = address.indexOf('@');
        if (index < 0) {
            return address;
        }
        return address.substring(0, index);
    }

    /**
     * 做简单检查
     */
    public static Boolean isMailAddressEasy(String mailAddress) {
        if (StringUtil.isEmpty(mailAddress) || mailAddress.length() > 127) {
            return false;
        }
        String[] strs = mailAddress.split("\\@");
        if (strs.length < 2) {
            return false;
        }
        String[] subStrs = strs[1].split("\\.");
        if (subStrs.length < 2) {
            return false;
        }
        return true;
    }

    /**
     * EWEI5.0统一的验证方式
     * 
     * @param email
     * @see com.ewei.support.mail.util.MailUtil.isMailAddress(String)
     * @see /ewei-web/src/main/webapp/source/scripts/propcare.js verifyEmailAddress()
     * @see /ewei-admin/src/main/webapp/source/scripts/propcare.js verifyEmailAddress()
     */
    public static Boolean isMailAddress(String mailAddress) {
        if (StringUtil.isEmpty(mailAddress) || mailAddress.length() > 127) {
            return false;
        }

        Matcher matcher = MAIL_ADDRESS_PATTERN.matcher(mailAddress);
        return matcher.matches();
    }

    public static String getDomain(String mailAddress) {
        if (isMailAddress(mailAddress)) {
            int indexOf = mailAddress.indexOf('@');
            return mailAddress.substring(indexOf + 1);
        }

        return "";
    }

    public static String guessSendHost(String address) {
        String suffix = getSuffix(address);
        String lowerCase = StringUtil.toLowerCase(suffix);
        return SEND_HOSTS.get(lowerCase);
    }

    public static boolean isAllMailAddressEasy(List<String> mailAddresses) {
        for (String mailAddress : mailAddresses) {
            if (!isMailAddressEasy(mailAddress)) {
                return false;
            }
        }
        return true;
    }
}